<?php
// @formatter:off
/**
 * @file s.collections.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: s.collections.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/services/s.collections.php $
 */
// @formatter:on

$wcServiceStatus = false;
$wcServiceErrorCode = 500;

if(defined('__SERVICE__') && $wcGroup && !$wcGroupForbidden && $wcProfile->allowedAny(WC_PERM_COMIC_VIEW, WC_PERM_COLLECTION_LIST, WC_PERM_COMIC_SET_COLLECTION, WC_PERM_COLLECTION_ADD, WC_PERM_COLLECTION_EDIT, WC_PERM_COLLECTION_RENAME)) {
	/*
	 * Conditions:
	 *
	 * Request Method GET:
	 *	- When there's a comic id specified ($wcComic) and the requesting
	 *	  user is allowed to view comics, the result is the comic's
	 *	  collection id and name.
	 *	  Otherwise, if the comic belong to no collection or if the
	 *	  collection is broken, returns an error.
	 *	- When there's a collection id specified ($wcCollection) and the
	 *	  requesting user is allowed to list collections, the result is
	 *	  the list of sub-collections belonging to the specified one.
	 *	  When the specified collection is a leaf, tag "wc-result" is
	 *	  returned empty.
	 *	- When there's neither comic nor collection specified and the
	 *	  requesting user is allowed to list collections, the service
	 *	  returns the list of top collections.
	 *	  Where there are no top collections tag "wc-result" is returned
	 *	  empty.
	 *	- When there's neither comic not directory set, but there's a
	 *	  parameter called "pattern" set and the requesting user is
	 *	  allowed to view and list collections, the service returns the
	 *	  list of collections that match with the specified pattern.
	 *	- In other case, it returns an parameters error message.
	 *
	 * Request Method POST:
	 *	- When there's a comic id specified ($wcComic), the requesting
	 *	  user is allowed to set comic's collection and a parameter called
	 *	  COLLECTION is set in post data, the service will:
	 *		- attempt to assign the collection to the comic.
	 *		- attempt to assign no collection to the comic if
	 *		  COLLECTION is zero.
	 *	- When there's a collection id specified ($wcCollection), the
	 *	  requesting user is allowed to edit collections and a parameter
	 *	  called COLLECTION is set in post data, the service will:
	 *		- attempt to assign the collection to $wcCollection.
	 *		- attempt to assign no collection to $wcCollection if
	 *		  COLLECTION is zero.
	 *	- When there's the parameter "create" ($_REQUEST), the
	 *	  requesting user is allowed to add a collection and a parameter
	 *	  called NAME is set in post data, the service will attempt to
	 *	  create a new collection (names can be duplicated). As a result,
	 *	  tag "wc-result" will contain the new collection's name and id.
	 *	- When there's the parameter "rename" ($_REQUEST), there's a
	 *	  collection specified ($wcCollection), the requesting user is
	 *	  allowed to rename collections and a parameter called NEXT_NAME
	 *	  is set in post data, the service will attempt to rename the
	 *	  collection with the new name.
	 *	- When there's a collection id specified ($wcCollection), the
	 *	  requesting user is allowed to remove collections and a parameter
	 *	  called "remove" is set in get data, the service will attempt to
	 *	  delete the collection.
	 *	- In other case, it returns an parameters error message.
	 */
	if($_SERVER["REQUEST_METHOD"] == "GET") {
		if($wcComic && $wcProfile->allowedTo(WC_PERM_COMIC_VIEW)) {
			//
			// Comic tags list requested.
			if($wcComic->collection) {
				$collection = @$wcCollectionsHolder->item($wcComic->collection, $wcGroup);
				if($collection && $collection->ok()) {
					$wcServiceStatus = true;
					$wcServiceXMLResult .= "<collection>";
					$wcServiceXMLResult .= "<id>{$collection->id}</id>";
					$wcServiceXMLResult .= "<name>{$collection->name}</name>";
					$wcServiceXMLResult .= "<path>{$collection->path()}</path>";
					$wcServiceXMLResult .= "</collection>";
				} else {
					$wcServiceErrorMsg = "Comic collection '{$wcComic->collection}' seems to be not valid";
				}
			} else {
				$wcServiceErrorMsg = "Comic belong to no collection";
			}
		} elseif($wcCollection && $wcProfile->allowedTo(WC_PERM_COLLECTION_LIST)) {
			//
			// Collection's collections list requested.
			$wcServiceStatus = true;

			$items = $wcCollection->collections();
			uasort($items, "WCCollection::Sort");
			foreach($items as $collection) {
				$wcServiceXMLResult .= "<collection>";
				$wcServiceXMLResult .= "<id>{$collection->id}</id>";
				$wcServiceXMLResult .= "<name>{$collection->name}</name>";
				$wcServiceXMLResult .= "<path>{$collection->path()}</path>";
				$wcServiceXMLResult .= "</collection>";
			}
		} elseif(isset($_REQUEST["pattern"])) {
			global $wcCollectionsHolder;
			global $wcPageCache;

			$wcPageCache->anyCollection();

			$pattern = $_REQUEST["pattern"];
			$limit = isset($_REQUEST["limit"]) ? $_REQUEST["limit"] : 20;

			$wcServiceStatus = true;
			foreach($wcCollectionsHolder->collectionsLike($pattern, $wcGroup) as $collection) {
				$wcServiceXMLResult .= "<collection>";
				$wcServiceXMLResult .= "<id>{$collection->id}</id>";
				$wcServiceXMLResult .= "<name>{$collection->name}</name>";
				$wcServiceXMLResult .= "<path>{$collection->path()}</path>";
				$wcServiceXMLResult .= "</collection>";

				$limit--;
				if(!$limit) {
					break;
				}
			}
		} elseif($wcProfile->allowedTo(WC_PERM_COLLECTION_LIST)) {
			//
			// Return the list of top collections.
			$wcServiceStatus = true;

			foreach($wcCollectionsHolder->topItems($wcGroup) as $collection) {
				$wcServiceXMLResult .= "<collection>";
				$wcServiceXMLResult .= "<id>{$collection->id}</id>";
				$wcServiceXMLResult .= "<name>{$collection->name}</name>";
				$wcServiceXMLResult .= "<path>{$collection->path()}</path>";
				$wcServiceXMLResult .= "</collection>";
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} elseif($_SERVER["REQUEST_METHOD"] == "POST") {
		if($wcComic && isset($_POST["COLLECTION"]) && $wcProfile->allowedTo(WC_PERM_COMIC_SET_COLLECTION)) {
			if($_POST["COLLECTION"] == 0) {
				$collection = @$wcCollectionsHolder->item($wcComic->collection, $wcGroup);
				if($collection && $collection->ok()) {
					//
					// Eliminating comic collection.
					$collection->removeComic($wcComic);
					$wcServiceStatus = true;
				} else {
					$wcServiceErrorMsg = "Collection id {$wcComic->collection} is not valid";
				}
			} else {
				$collection = @$wcCollectionsHolder->item($_POST["COLLECTION"], $wcGroup);
				if($collection && $collection->ok()) {
					//
					// Setting comic collection.
					$collection->addComic($wcComic);
					$wcServiceStatus = true;
				} else {
					$wcServiceErrorMsg = "Collection id {$_POST["COLLECTION"]} is not valid";
				}
			}
		} elseif($wcCollection && isset($_POST["COLLECTION"]) && $wcProfile->allowedTo(WC_PERM_COLLECTION_EDIT)) {
			if($_POST["COLLECTION"] == 0) {
				$collection = @$wcCollectionsHolder->item($wcCollection->parent, $wcGroup);
				if($collection && $collection->ok()) {
					//
					// Eliminating comic collection.
					$collection->removeCollection($wcCollection);
					$wcServiceStatus = true;
				} else {
					$wcServiceErrorMsg = "Collection id {$wcCollection->parent} is not valid";
				}
			} else {
				$collection = @$wcCollectionsHolder->item($_POST["COLLECTION"], $wcGroup);
				if($collection && $collection->ok()) {
					if($wcCollection->id != $collection->id) {
						//
						// Setting comic collection.
						$collection->addCollection($wcCollection);
						$wcServiceStatus = true;
					} else {
						$wcServiceErrorMsg = "A collection can't be assigned to itself";
					}
				} else {
					$wcServiceErrorMsg = "Collection id {$_POST["COLLECTION"]} is not valid";
				}
			}
		} elseif(isset($_REQUEST["create"]) && isset($_POST["NAME"]) && strlen(trim($_POST["NAME"])) && $wcProfile->allowedTo(WC_PERM_COLLECTION_ADD)) {
			$name = trim($_POST["NAME"]);

			$collection = @$wcCollectionsHolder->create($name, $wcGroup);
			if($collection) {
				$wcServiceStatus = true;
				$wcServiceXMLResult .= "<collection>";
				$wcServiceXMLResult .= "<id>{$collection->id}</id>";
				$wcServiceXMLResult .= "<name>{$collection->name}</name>";
				$wcServiceXMLResult .= "<path>{$collection->path()}</path>";
				$wcServiceXMLResult .= "</collection>";
			} else {
				$wcServiceErrorMsg = "Unable to create collection '{$name}'";
			}
		} elseif(isset($_REQUEST["rename"]) && isset($_POST["NEXT_NAME"]) && strlen(trim($_POST["NEXT_NAME"])) && $wcProfile->allowedTo(WC_PERM_COLLECTION_RENAME)) {
			if($wcCollection && $wcCollection->ok()) {
				$next = trim($_POST["NEXT_NAME"]);
				$name = $wcCollection->name;

				if(!$next) {
					$wcServiceErrorMsg = "No new name specified";
				} elseif($next == $name) {
					$wcServiceErrorMsg = "New name is the same than current one";
				} else {
					$wcServiceStatus = true;
					$wcCollection->name = $next;
				}
			} else {
				$wcServiceErrorMsg = "This service was call with invalid parameters";
			}
		} elseif($wcCollection && isset($_REQUEST["remove"]) && $wcProfile->allowedTo(WC_PERM_COLLECTION_REMOVE)) {
			if($wcCollection->remove()) {
				$wcServiceStatus = true;
			} else {
				$wcServiceErrorMsg = "Unable to remove collection '{$name}'";
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	}
} else {
	$wcServiceErrorCode = 403;
	$wcServiceErrorMsg = "Unable to access service";
}
?>